/**
 * -----------------------------------
 *  Copyright (c) 2021-2025
 *  All rights reserved, Designed By www.linfengtech.cn
 * 林风社交论坛商业版本请务必保留此注释头信息
 * 商业版授权联系技术客服	 QQ:  3582996245
 * 严禁分享、盗用、转卖源码或非法牟利！
 * 版权所有 ，侵权必究！
 * -----------------------------------
 */
package io.linfeng.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.linfeng.common.utils.RedisUtils;
import io.linfeng.modules.app.dao.FriendDao;
import io.linfeng.modules.app.param.ClearChatMessageUnreadForm;
import io.linfeng.modules.app.param.GetChatListForm;
import io.linfeng.modules.app.websocket.constant.MessageConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.linfeng.common.utils.PageUtils;
import io.linfeng.common.utils.Query;

import io.linfeng.modules.app.dao.ChatMessageDao;
import io.linfeng.modules.app.entity.ChatMessageEntity;
import io.linfeng.modules.app.service.ChatMessageService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("chatMessageService")
public class ChatMessageServiceImpl extends ServiceImpl<ChatMessageDao, ChatMessageEntity> implements ChatMessageService {


    @Resource
    private FriendDao friendDao;

    @Resource
    private ChatMessageDao chatMessageDao;

    @Autowired
    private RedisUtils redisUtils;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ChatMessageEntity> page = this.page(
                new Query<ChatMessageEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void saveMessage(ChatMessageEntity chatMessage) {
        this.save(chatMessage);
        redisUtils.delete(MessageConstant.IM_CHAT+chatMessage.getSessionId());
        String lastMessage ;
        switch (chatMessage.getMessageType()) {
            case "image":
                lastMessage = "【图片】";
                break;
            case "file":
                lastMessage = "【文件】";
                break;
            case "video":
                lastMessage = "【视频】";
                break;
            default:
                lastMessage = chatMessage.getContent();
        }
        friendDao.updateInfo(chatMessage.getSessionId(), lastMessage);

    }

    @Override
    public void withdrawMessage(String messageId) {
        ChatMessageEntity message = this.getById(messageId);
        chatMessageDao.withdrawMessage(messageId);
        redisUtils.delete(MessageConstant.IM_CHAT+message.getSessionId());
        //修改好友表最新消息展示 如果有消息撤回就显示
        String lastMessage="撤回了一条消息";
        friendDao.updateWithdrawInfo(message.getSessionId(), lastMessage);
    }

    @Override
    public IPage<ChatMessageEntity> getChatMessageList(GetChatListForm param) {
        IPage<ChatMessageEntity> page = new Page<>(param.getPageNum(),param.getPageSize());
        LambdaQueryWrapper<ChatMessageEntity> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatMessageEntity::getSessionId,param.getSessionId());
        queryWrapper.orderByDesc(ChatMessageEntity::getId);
        if(param.getLastMessageId()!=null){
            queryWrapper.le(ChatMessageEntity::getId, param.getLastMessageId());
        }
        return this.baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public void deleteChatMessage(Integer friendId, Integer myId) {
        LambdaQueryWrapper<ChatMessageEntity> wrapper1=new LambdaQueryWrapper<>();
        wrapper1.eq(ChatMessageEntity::getSenderId,friendId);
        wrapper1.eq(ChatMessageEntity::getReceiverId,myId);
        this.remove(wrapper1);
        LambdaQueryWrapper<ChatMessageEntity> wrapper2=new LambdaQueryWrapper<>();
        wrapper2.eq(ChatMessageEntity::getSenderId,myId);
        wrapper2.eq(ChatMessageEntity::getReceiverId,friendId);
        this.remove(wrapper2);
    }


}